
local TurnInEverything = LibStub("AceAddon-3.0"):GetAddon("TurnInEverything")
--local L = LibStub("AceLocale-3.0"):GetLocale("TurnInEverything")

local options, configOptions = nil, {}
--[[ This options table is used in the GUI config. ]]-- 
local function getOptions() 
	if not options then
		options = {
		    type = "group",
			name ="TurnInEverything",			
		    args = {
				general = {
					order = 1,
					type = "group",
					name = "General",
					get = function(info)
						local key = info.arg or info[#info]
						TurnInEverything:SendDebugMsg("getSettings: "..key.." :: "..tostring(TurnInEverything.db.profile[key]))
						return TurnInEverything.db.profile[key]
					end,
					set = function(info, value)
						local key = info.arg or info[#info]
						TurnInEverything.db.profile[key] = value
						TurnInEverything:SendDebugMsg("setSettings: "..key.." :: "..tostring(TurnInEverything.db.profile[key]))
					end,
					args = {
						enable = {
							order = 1,
							type = "toggle",
							name = "Enable", --L["Enable"],
							width = "normal"
				        },
						modifier = {
							order = 2,
							type = "select",
							name = "Modifier", --L["Modifier"],
							values = { ALT="ALT", CTRL="CTRL", SHIFT="SHIFT" }, --{ ALT=L["ALT"], CTRL=L["CTRL"], SHIFT=L["SHIFT"] }
							width = "normal"
						},
						tournReward = {
							order = 3,
							type = "select",
							name = "Argent Tournament Reward", --L["Argent Tournament Reward"],
							values = { NONE="None", WRIT="Writ", PURSE="Purse" }, --{ NONE=L["None"], WRIT=L["Writ"], PURSE=L["Purse"] },
							style = "radio"
						},
						modedesc = {
							order = 4,
							type = "description",
							fontSize = "medium",
							name = "Gold Mode will select the highest-value item.  Usable Mode will automatically pick a single usable item and is manual select if there are multiple usable.  Usable, Highest-Value is similar to Usable Mode except a manual select if the single usable item is not also the highest-value item.",
						},
						mode = {
							order = 5,
							type = "select",
							name = "How do you want TurnInEveryting to behave?", --L["Modes"],
							values = { GOLD="Gold Mode", USABLE="Usable Mode", USABLE_HIGHEST_VALUE="Usable, Highest-Value" }, --{ GOLD=L["Gold Mode"], USABLE=L["Usable Mode"] },
							style = "radio"
				        },
						debug = {
							order = -1,
							type = "toggle",
							name = "Debug" --L["Debug"]
				        }
					}
				}
		    }
		}
		for k,v in pairs(configOptions) do
			options.args[k] = (type(v) == "function") and v() or v
		end
	end
	
	return options
end

local function openConfig() 
	InterfaceOptionsFrame_OpenToCategory(TurnInEverything.optionsFrames.Profiles)
	InterfaceOptionsFrame_OpenToCategory(TurnInEverything.optionsFrames.TurnInEverything)
	InterfaceOptionsFrame:Raise()
end

function TurnInEverything:SetupOptions()
	self.optionsFrames = {}

	LibStub("AceConfigRegistry-3.0"):RegisterOptionsTable("TurnInEverything", getOptions)
	self.optionsFrames.TurnInEverything = LibStub("AceConfigDialog-3.0"):AddToBlizOptions("TurnInEverything", nil, nil, "general")

	configOptions["Profiles"] = LibStub("AceDBOptions-3.0"):GetOptionsTable(self.db)

	self.optionsFrames["Profiles"] = LibStub("AceConfigDialog-3.0"):AddToBlizOptions("TurnInEverything", "Profiles", "TurnInEverything", "Profiles")

	LibStub("AceConsole-3.0"):RegisterChatCommand("tie", openConfig)
end